package com.sz.virtualbank.service.system.impl;

import com.sz.virtualbank.dao.entity.SystemParam;
import com.sz.virtualbank.dao.mapper.SystemParamMapper;
import com.sz.virtualbank.service.system.SystemParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 系统参数服务实现类
 * 
 * @author Claude
 */
@Service
@Slf4j
public class SystemParamServiceImpl implements SystemParamService {

    @Autowired
    private SystemParamMapper systemParamMapper;
    
    @Override
    @Cacheable(value = "systemParamCache", key = "'paramValue:' + #paramKey")
    public String getParamValue(String paramKey) {
        SystemParam param = systemParamMapper.selectByParamKey(paramKey);
        return param != null ? param.getParamValue() : null;
    }

    @Override
    @Cacheable(value = "systemParamCache", key = "'param:' + #paramKey")
    public SystemParam getSystemParam(String paramKey) {
        return systemParamMapper.selectByParamKey(paramKey);
    }

    @Override
    @CacheEvict(value = "systemParamCache", allEntries = true)
    public boolean setSystemParam(String paramKey, String paramValue, String paramDesc) {
        SystemParam param = systemParamMapper.selectByParamKey(paramKey);
        
        if (param != null) {
            // 更新已有参数
            param.setParamValue(paramValue);
            param.setParamDesc(paramDesc);
            param.setUpdateTime(LocalDateTime.now());
            systemParamMapper.update(param);
        } else {
            // 创建新参数
            param = new SystemParam();
            param.setParamKey(paramKey);
            param.setParamValue(paramValue);
            param.setParamDesc(paramDesc);
            param.setCreateTime(LocalDateTime.now());
            param.setUpdateTime(LocalDateTime.now());
            systemParamMapper.insert(param);
        }
        
        return true;
    }

    @Override
    @CacheEvict(value = "systemParamCache", allEntries = true)
    public boolean updateSystemParam(String paramKey, String paramValue) {
        SystemParam param = systemParamMapper.selectByParamKey(paramKey);
        
        if (param == null) {
            return false;
        }
        
        param.setParamValue(paramValue);
        param.setUpdateTime(LocalDateTime.now());
        
        return systemParamMapper.update(param) > 0;
    }

    @Override
    public List<SystemParam> getAllSystemParams() {
        return systemParamMapper.selectAll();
    }

    @Override
    public Map<String, String> getSystemParams(List<String> paramKeys) {
        List<SystemParam> params = systemParamMapper.selectByParamKeys(paramKeys);
        
        Map<String, String> result = new HashMap<>();
        for (SystemParam param : params) {
            result.put(param.getParamKey(), param.getParamValue());
        }
        
        return result;
    }

    @Override
    @CacheEvict(value = "systemParamCache", allEntries = true)
    public boolean deleteSystemParam(String paramKey) {
        return systemParamMapper.deleteByParamKey(paramKey) > 0;
    }
}
